# -*- coding: utf-8 -*-
"""
Created on Wed Dec 28 09:18:27 2016

@author: lihan
"""
import numpy as np
from numpy import exp


def mapminmax(x, ymin, ymax):
    x = np.array(x)
    x.dtype = float
    y = (ymax - ymin) * (x - x.min()) / (x.max() - x.min()) + ymin
    return y


def risk(l1, l2, l3):
    l1 = int(l1 * 10)
    l2 = int(l2 * 10)
    l3 = int(l3 * 10)
    x = []
    y = []

    for i in range(700):
        x.append(0.1 * (i + 1))
        y.append(0.065 * x[i] ** (-0.5) * exp(-0.6 * x[i]) - 0.156 * (x[i] ** 0.5) * exp(-0.6 * x[i]) + 0.0312 * (
            x[i] ** 1.5) * exp(-0.6 * x[i]))
    z = mapminmax(y, 0, 1)

    rmax = 0.3
    rmin = 0.1
    rfinal = rmax ** 4
    p = []
    zz = np.zeros((700,))

    for ii in range(l1):
        il1 = round(((ii + 1) / float(l1)) * 12)
        zz[ii] = (z[il1 - 1])

    for ii in range(l1, l2):
        il2 = round((ii - l1 + 1) / float(l2 - l1) * 34)
        zz[ii] = (z[il2 + 12 - 1])
    for ii in range(l2, l3):
        il3 = round((ii - l2 + 1) / float(l3 - l2) * 23)
        zz[ii] = (z[il3 + 46 - 1])

    for ii in range(l3, 700):
        il4 = round((ii - l3 + 1) / float(700 - l3) * 631)
        zz[ii] = (z[il4 + 69 - 1])

    zz = 1 - zz
    for i in range(l1):
        p.append(rmax * zz[i])
    for i in range(l1, l2):
        p.append(rmax + (zz[i] - 1) * (rmax - rmin) / (1 - zz[l2 - 1]))
    for i in range(l2, 700):
        p.append((zz[i] - zz[l2 - 1]) * (rfinal - rmin) / (zz[699] - zz[l2 - 1]) + rmin)
    p = mapminmax(p, rmin, rmax)
    return [x, p]
